Technical Q&A HW54
if and then in Forth


Q: Why does the Forth word if require a then?

A: Unlike languages such as C, Forth requires an if to be followed by an else followed by a then. In the case without an else, the then must still follow an if. Since any character, number, or symbol can be a Forth word, you cannot use a pair of parentheses to encase the code following an if or else. The left paren character, (, is a Forth word. It tells the Forth interpreter to stop interpreting input and collect all text, as comments, until a right paren character, ), is encountered. A more rigorous definition for the if word is:

The then is the delimiter to the true or false function, since the true action follows the if statement and the false action follows the else statement. Other non-conditional code can follow the "if then" clause before the end of the definition.

[Mar 29 1999]


Developer Documentation | Technical Notes | Development Kits | Sample Code